﻿// -----------------------------------------------------------------------
// <copyright file="Status_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Status_DAL :BaseDAL
    {
        // Lấy tất cả Status
        public static List<Status> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Status);
            return DB.Status.Where(n => n.Ship_ID == shipID).ToList();
        }
        // Lấy Status theo Id
        public static Status GetDataById(int id)
        {
            return DB.Status.Where(s => s.Status_ID == id).FirstOrDefault();
        }
        //Thêm một Status
        public static bool Insert(Status status)
        {
            try
            {
                DB.Status.InsertOnSubmit(status);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một status
        public static bool Delete(Status status)
        {
            try
            {
                DB.Status.DeleteOnSubmit(status);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật status
        public static bool Update(Status status)
        {
            try
            {
                //Status objStatus = GetDataById(status.Status_ID);
                //objStatus.NameStatus = status.NameStatus;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
       
        // Kiểm tra xem Status Name có tồn tại chưa ?
        public static bool IsExits(Status status)
        {
            Status objStatus = null;
            objStatus = DB.Status.Where(m => (m.NameStatus == status.NameStatus) && 
                (m.Status_ID != status.Status_ID) && (m.Ship_ID == status.Ship_ID)).FirstOrDefault();
            return objStatus != null ? true : false;
        }
        public static bool IsRelationship(Status status)
        {
            Equipment equipment = DB.Equipments.Where(n => n.Status_ID == status.Status_ID).FirstOrDefault();
            if (null != equipment)
                return false;


            return true;
        }
    }
}
